/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import DAO.DAO_ChuyenTau;
import DBTransmit.TableChuyenTauRecord;
import java.util.ArrayList;
import java.util.List;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
import ticketbooking.Utils;

/**
 *
 * @author LEHOANGKHANH
 */
public class QLDSChuyenTau extends javax.swing.JInternalFrame {

    /**
     * Creates new form QLDSChuyenTau
     */
    SecretKey secretKey =  null;
    public QLDSChuyenTau(SecretKey key) {
        initComponents();
        this.secretKey = key;
        InitTable_ComboBox();
    }
    public void InitTable_ComboBox(){
        List<TableChuyenTauRecord> lst = DAO_ChuyenTau.LoadDS(secretKey);
        Utils.LoadDataToTable(jTable1, lst);
        List<String> lstMa = new ArrayList<String>();
        if(lst==null){
            return;
        }
        for(int i=0; i<lst.size(); i++){
            lstMa.add(lst.get(i).getMaCT());
        }
        Utils.LoadDataToComboBox(gadi, lstMa);
        Utils.LoadDataToComboBox(gaden, lstMa);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        mact = new javax.swing.JTextField();
        gadi = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        gaden = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        gio = new javax.swing.JTextField();
        ngay = new javax.swing.JTextField();
        slve = new javax.swing.JTextField();
        slvetk = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        sua = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        ok = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Quản Lý Danh Sách Chuyến Tàu");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Mã Chuyến Tàu:");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 204));
        jLabel1.setText("Quản Lý Danh Sách Chuyến Tàu");
        jLabel1.setToolTipText("");

        mact.setEditable(false);
        mact.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        mact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mactActionPerformed(evt);
            }
        });

        gadi.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        gadi.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Ga Đi:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Ga Đến:");

        gaden.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        gaden.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setText("Số Lượng Vé");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel7.setText("Giờ Khởi Hành:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setText("Ngày Khởi Hành:");

        gio.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        ngay.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        slve.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        slvetk.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel8.setText("Số Lượng Vé Mỗi Tài Khoản:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Mã Chuyến Tàu", "Ga Đi", "Ga Đến", "Ngày Khởi Hành", "Giờ Khởi Hành", "Số Lượng Vé", "Số Lượng Vé Mỗi TK"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.getTableHeader().setReorderingAllowed(false);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/add.png"))); // NOI18N
        jButton1.setText("Thêm");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        sua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/modify.png"))); // NOI18N
        sua.setText("Sửa");
        sua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                suaActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/erase.png"))); // NOI18N
        jButton3.setText("Xóa");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/1403367972_clean.png"))); // NOI18N
        ok.setEnabled(false);
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addComponent(jLabel4)
                        .addGap(51, 51, 51))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(gadi, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(gaden, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(mact, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE))
                        .addGap(47, 47, 47)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(31, 31, 31)
                                .addComponent(slvetk, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6))
                                .addGap(100, 100, 100)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(ngay, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(gio, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(slve, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(26, 26, 26)
                                .addComponent(ok, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))))))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 811, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(sua)
                .addGap(18, 18, 18)
                .addComponent(jButton3)
                .addGap(250, 250, 250))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(gio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(mact, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel2)
                                .addComponent(jLabel5))
                            .addComponent(ok))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(gadi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel7))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(gaden, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel6)
                                    .addComponent(slve, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(ngay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(slvetk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addGap(13, 13, 13)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(sua)
                    .addComponent(jButton3))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mactActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_mactActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        mact.setText("");
        ngay.setText("");
        gio.setText("");
        slve.setText("");
        slvetk.setText("");
        ok.setEnabled(true);
        sua.setEnabled(false);
        mact.setEditable(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void suaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_suaActionPerformed
        // TODO add your handling code here:
        TableChuyenTauRecord record = new TableChuyenTauRecord();
        record.setMaCT(mact.getText());
        record.setGaDi(gadi.getSelectedItem().toString());
        record.setGaDen(gaden.getSelectedItem().toString());
        record.setNgayKhoiHanh(ngay.getText());
        record.setGioKhoiHanh(gio.getText());
        record.setSLVe(Integer.parseInt(slve.getText().toString()));
        record.setSLVeMoiTaiKhoan(Integer.parseInt(slvetk.getText().toString()));
        boolean state = DAO_ChuyenTau.Update(record, secretKey);
        if(state == true){
            JOptionPane.showMessageDialog(rootPane, "Sửa dữ liệu thành công!");
            InitTable_ComboBox();
        }
        else{
            JOptionPane.showMessageDialog(rootPane, "Sửa dữ liệu thất bại!");
        }
    }//GEN-LAST:event_suaActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        boolean state = DAO_ChuyenTau.Delete(mact.getText(), secretKey);
        if(state == true){
            JOptionPane.showMessageDialog(rootPane, "Xóa dữ liệu thành công");
            InitTable_ComboBox();
        }
        else{
            JOptionPane.showMessageDialog(rootPane, "Xóa dữ liệu thất bại");
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        // TODO add your handling code here:
        if(mact.getText().equals("")){
            JOptionPane.showMessageDialog(rootPane, "Chưa nhập mã chuyến tàu");
            return;
        }
        TableChuyenTauRecord record = new TableChuyenTauRecord();
        record.setMaCT(mact.getText());
        record.setGaDi(gadi.getSelectedItem().toString());
        record.setGaDen(gaden.getSelectedItem().toString());
        record.setNgayKhoiHanh(ngay.getText());
        record.setGioKhoiHanh(gio.getText());
        record.setSLVe(Integer.parseInt(slve.getText().toString()));
        record.setSLVeMoiTaiKhoan(Integer.parseInt(slvetk.getText().toString()));
        boolean state = DAO_ChuyenTau.Update(record, secretKey);
        if(state == true){
            JOptionPane.showMessageDialog(rootPane, "Thêm dữ liệu thành công!");
            InitTable_ComboBox();
            ok.setEnabled(false);
            sua.setEnabled(true);
            mact.setEditable(false);
        }
        else{
            JOptionPane.showMessageDialog(rootPane, "Thêm dữ liệu thất bại!");
            ok.setEnabled(false);
            sua.setEnabled(true);
            mact.setEditable(false);
        }
    }//GEN-LAST:event_okActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        int rowIndex = jTable1.getSelectedRow();
        if(rowIndex == -1){
            return;
        }
        mact.setText(jTable1.getValueAt(rowIndex, 0).toString());
        gadi.setSelectedItem(jTable1.getValueAt(rowIndex, 1).toString());
        gaden.setSelectedItem(jTable1.getValueAt(rowIndex, 2).toString());
        ngay.setText(jTable1.getValueAt(rowIndex, 3).toString());
        gio.setText(jTable1.getValueAt(rowIndex, 4).toString());
        slve.setText(jTable1.getValueAt(rowIndex, 5).toString());
        slvetk.setText(jTable1.getValueAt(rowIndex, 6).toString());
        
    }//GEN-LAST:event_jTable1MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox gaden;
    private javax.swing.JComboBox gadi;
    private javax.swing.JTextField gio;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField mact;
    private javax.swing.JTextField ngay;
    private javax.swing.JButton ok;
    private javax.swing.JTextField slve;
    private javax.swing.JTextField slvetk;
    private javax.swing.JButton sua;
    // End of variables declaration//GEN-END:variables
}
